﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace CarlowPitBoy
{
    class Button
    {

        private Image _unclickedImage;
        private Image _clickedImage;
        private Image _drawnImage;
        private bool _active = false;
        private bool _mouseProxRequired = false;
       // private KeyInfo[] keysRequired;
        //Christina rules!
        public int xPos;
        public int yPos;
        public bool visible = true;

        public Image drawnImage
        {
            get
            {
                return _drawnImage;
            }
        }

        /// <summary>
        /// Creates a button activated by a click of a given key
        /// </summary>
        /// <param name="unclickedImage">Image that appears when button isn't being clicked</param>
        /// <param name="clickedImage">Image that appears when button is clicked</param>
        /// <param name="mouseButton">Which Key or Keys(from KeyReference) you want to be pressed to activate the button</param>
        public Button(Image unclickedImage, Image clickedImage)
        {
            _mouseProxRequired = true;

            _clickedImage = clickedImage;
            _unclickedImage = unclickedImage;
            _drawnImage = _unclickedImage;
        }

        /// <summary>
        /// Creates a button activated by a click of a given key
        /// </summary>
        /// <param name="unclickedImage">Image that appears when button isn't being clicked</param>
        /// <param name="clickedImage">Image that appears when button is clicked</param>
        /// <param name="mouseButton">Which Key or Keys(from KeyReference) you want to be pressed to activate the button</param>
        public Button(Image unclickedImage, Image clickedImage, int xPos, int yPos)
        {
            _mouseProxRequired = true;

            _clickedImage = clickedImage;
            _unclickedImage = unclickedImage;
            _drawnImage = _unclickedImage;
            this.xPos = xPos;
            this.yPos = yPos;
        }

        /// <summary>
        /// Creates a button activated by a click of a given key
        /// </summary>
        /// <param name="unclickedImage">Image that appears when button isn't being clicked</param>
        /// <param name="clickedImage">Image that appears when button is clicked</param>
        /// <param name="mouseButton">Which Key or Keys(from KeyReference) you want to be pressed to activate the button</param>
        /// <param name="mouseProximityRequired">Whether mouse needs to be hovering over the button or not to activate it</param>
        public Button(Image unclickedImage, Image clickedImage, bool mouseProximityRequired)
        {
            _mouseProxRequired = mouseProximityRequired;

            _clickedImage = clickedImage;
            _unclickedImage = unclickedImage;
            _drawnImage = _unclickedImage;
        }

        /// <summary>
        /// Creates a button activated by a click of a given key
        /// </summary>
        /// <param name="unclickedImage">Image that appears when button isn't being clicked</param>
        /// <param name="clickedImage">Image that appears when button is clicked</param>
        /// <param name="mouseButton">Which Key or Keys(from KeyReference) you want to be pressed to activate the button</param>
        /// <param name="mouseProximityRequired">Whether mouse needs to be hovering over the button or not to activate it</param>
        public Button(Image unclickedImage, Image clickedImage, int xPos, int yPos, bool mouseProximityRequired)
        {
            _mouseProxRequired = mouseProximityRequired;

            _clickedImage = clickedImage;
            _unclickedImage = unclickedImage;
            _drawnImage = _unclickedImage;
            this.xPos = xPos;
            this.yPos = yPos;
        }

        /// <summary>
        /// Creates a button activated by a click of a given key
        /// </summary>
        /// <param name="unclickedImage"></param>
        /// <param name="clickedImage"></param>
        /// <param name="mouseButton">Which Key or Keys(from KeyReference) you want to be pressed to activate the button</param>
        /// <param name="mouseProximityRequired">Whether mouse needs to be placed over the button or not to activate it</param>
        public Button(Image unclickedImage, Image clickedImage, KeyInfo[] buttons, bool mouseProximityRequired)
        {
            _mouseProxRequired = mouseProximityRequired;

            _clickedImage = clickedImage;
            _unclickedImage = unclickedImage;
            _drawnImage = _unclickedImage;
        }

        /// <summary>
        /// Creates a button activated by a click of a given key
        /// </summary>
        /// <param name="unclickedImage"></param>
        /// <param name="clickedImage"></param>
        /// <param name="mouseButton">Which Key or Keys(from KeyReference) you want to be pressed to activate the button</param>
        /// <param name="mouseProximityRequired">Whether mouse needs to be placed over the button or not to activate it</param>
        public Button(Image unclickedImage, Image clickedImage, int xPos, int yPos, KeyInfo[] buttons, bool mouseProximityRequired)
        {
            _mouseProxRequired = mouseProximityRequired;

            _clickedImage = clickedImage;
            _unclickedImage = unclickedImage;
            _drawnImage = _unclickedImage;
            this.xPos = xPos;
            this.yPos = yPos;
        }

        /// <summary>
        /// Check this when appropriate button is clicked
        /// </summary>
        /// <param name="xPointClicked">X Coordinate of where user clicked</param>
        /// <param name="yPointclicked">Y Coordinate of where user clicked</param>
        /// <returns></returns>
        public bool checkPressed(int xPointClicked, int yPointclicked)
        {
            if (_mouseProxRequired)
            {
                if (xPointClicked > xPos)
                    if (xPointClicked < (xPos + _drawnImage.Width))
                        if (yPointclicked > yPos)
                            if (yPointclicked < (yPos + _drawnImage.Height))
                            {
                                _active = true;
                                press();
                                return true;
                            }
            }
            else
            {
                _active = true;
                press();
                return true;
            }
            depress();
            return false;
        }

        public bool checkPressed(Point pointClicked)
        {
            if (_mouseProxRequired)
            {
                if (pointClicked.X > xPos)
                    if (pointClicked.X < (xPos + _drawnImage.Width))
                        if (pointClicked.Y > yPos)
                            if (pointClicked.Y < (yPos + _drawnImage.Height))
                            {
                                _active = true;
                                press();
                                return true;
                            }
            }
            else
            {
                _active = true;
                press();
                return true;
            }
            depress();
            return false;
        }

        public bool checkRepressed(int xPointClicked, int yPointClicked, KeyInfo keyToCheckFor)
        {
            if (_active)
                if (keyToCheckFor.isPressed)
                    if(_mouseProxRequired)
                    {
                    if (xPointClicked > xPos)
                        if (xPointClicked < (xPos + _drawnImage.Width))
                            if (yPointClicked > yPos)
                                if (yPointClicked < (yPos + _drawnImage.Height))
                                {
                                    press();
                                    return true;
                                }
                    }
                    else
                    {
                        press();
                        return true;
                    }
            _drawnImage = _unclickedImage;
            return false;
        }

        public bool checkRepressed(int xPointClicked, int yPointClicked, KeyInfo[] keysToCheckFor)
        {
            if (_active)
                for (byte keysChecked = 0; keysChecked < keysToCheckFor.Length; keysChecked++)
                {
                    if (keysToCheckFor[keysChecked].isPressed)
                    {
                        if (_mouseProxRequired)
                        {
                            if (xPointClicked > xPos)
                                if (xPointClicked < (xPos + _drawnImage.Width))
                                    if (yPointClicked > yPos)
                                        if (yPointClicked < (yPos + _drawnImage.Height))
                                        {
                                            press();
                                            return true;
                                        }
                        }
                        else
                        {
                            press();
                            return true;
                        }
                        break;
                    }
                }
            _drawnImage = _unclickedImage;
            return false;
        }

        public bool checkRepressed(Point pointClicked, KeyInfo keyToCheckFor)
        {
            if (_active)
                if (keyToCheckFor.isPressed)
                    if (_mouseProxRequired)
                    {
                        if (pointClicked.X > xPos)
                            if (pointClicked.X < (xPos + _drawnImage.Width))
                                if (pointClicked.Y > yPos)
                                    if (pointClicked.Y < (yPos + _drawnImage.Height))
                                    {
                                        press();
                                        return true;
                                    }
                    }
                    else
                    {
                        press();
                        return true;
                    }
            _drawnImage = _unclickedImage;
            return false;
        }

        public bool checkRepressed(Point pointClicked, KeyInfo[] keysToCheckFor)
        {
            if (_active)
                for (byte keysChecked = 0; keysChecked < keysToCheckFor.Length; keysChecked++)
                {
                    if (keysToCheckFor[keysChecked].isPressed)
                    {
                        if (_mouseProxRequired)
                        {
                            if (pointClicked.X > xPos)
                                if (pointClicked.X < (xPos + _drawnImage.Width))
                                    if (pointClicked.Y > yPos)
                                        if (pointClicked.Y < (yPos + _drawnImage.Height))
                                        {
                                            press();
                                            return true;
                                        }
                        }
                        else
                        {
                            press();
                            return true;
                        }
                        break;
                    }
                }
            _drawnImage = _unclickedImage;
            return false;
        }

        /// <summary>
        /// Call on button/mouse up event
        /// </summary>
        /// <param name="xPointClicked">Current X coordinate of mouse</param>
        /// <param name="yPointclicked">Current Y coordinate of mouse</param>
        /// <returns>Whether button press and depress was completed sucessfully</returns>
        public bool checkActivated(int xPointClicked, int yPointclicked)
        {
            if (_active)
                if (_mouseProxRequired)
                {
                    if (xPointClicked > xPos)
                        if (xPointClicked < (xPos + _drawnImage.Width))
                            if (yPointclicked > yPos)
                                if (yPointclicked < (yPos + _drawnImage.Height))
                                {
                                    return true;
                                }
                }
                else
                    return true;
            depress();
            return false;
        }

        //public bool checkActivated(int xPointClicked, int yPointclicked, KeyInfo[] keysToCheckFor)
        //{
        //    if(_active)
        //        for (byte keysChecked = 0; keysChecked < keysToCheckFor.Length; keysChecked++)
        //            if (keysToCheckFor[keysChecked].isPressed)
        //            {
        //                if (xPointClicked > xPos)
        //                    if (xPointClicked < (xPos + _drawnImage.Width))
        //                        if (yPointclicked > yPos)
        //                            if (yPointclicked < (yPos + _drawnImage.Height))
        //                            {
        //                                return true;
        //                            }
        //                break;
        //            }
        //    depress();
        //    return false;
        //}

        /// <summary>
        /// Call on button/mouse up event
        /// </summary>
        /// <param name="pointclicked">Current location of mouse</param>
        /// <returns>Whether button press and depress was completed sucessfully</returns>
        public bool checkActivated(Point pointClicked)
        {
            if (_active)
                if (_mouseProxRequired)
                {
                    if (pointClicked.X > xPos)
                        if (pointClicked.X < (xPos + _drawnImage.Width))
                            if (pointClicked.Y > yPos)
                                if (pointClicked.Y < (yPos + _drawnImage.Height))
                                {
                                    return true;
                                }
                }
                else
                    return true;
            depress();
            return false;
        }
        private void press()
        {
            _drawnImage = _clickedImage;
        }

        private void depress()
        {
            _drawnImage = _unclickedImage;
            _active = false;
        }

    }
}
